import { GET_ADDRESS, SET_CUR_PAGE, SET_EACH_PAGE, SEARCH_ADDRESS } from '../types';
import { handleActions } from 'redux-actions';
import _ from 'lodash';

const Initdata = {
    curPage: 1, //当前页
    eachPage: 10,//每页条数
    total: 0,
    maxPage: 0,
    rows: [],
}

export const address = handleActions({
    // 获取驿站全部数据
    [GET_ADDRESS]: (state, { payload }) => {
        const { total, maxPage, rows } = payload;
        // 后面的对象属性会覆盖前面的
        return _.assign({}, state, {
            total, maxPage, rows
        })
    },
    // 驿站当前页
    [SET_CUR_PAGE]: (state, { payload }) => {
        const { curPage } = payload;
        const $state = _.clone(state);
        $state.curPage = curPage
        return $state;
    },
    // 驿站每页显示条数
    [SET_EACH_PAGE]: (state, { payload }) => {
        const { eachPage } = payload;
        const $state = _.clone(state);
        $state.eachPage = eachPage
        return $state;
    },

    // 查询驿站数据
    [SEARCH_ADDRESS]: (state, { payload }) => {
        const { data } = payload
        // 查找拿到data跟total
        return _.assign({}, state, { rows: data, total: data.length })
    }

}, Initdata)
